package com.jet.goods.controller;

import com.jet.core.model.vo.PageVo;
import com.jet.core.result.ResultJson;
import com.jet.goods.domain.dto.UnitDto;
import com.jet.goods.domain.entity.GoodsUnit;
import com.jet.goods.domain.query.UnitQuery;
import com.jet.goods.domain.vo.UnitVo;
import com.jet.goods.service.IGoodsUnitService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods/unit")
public class UnitController {

    private final IGoodsUnitService goodsUnitService;

    public UnitController(IGoodsUnitService goodsUnitService) {
        this.goodsUnitService = goodsUnitService;
    }

    @PostMapping
    public ResultJson<Void> create(@RequestHeader long merchantNo,
                                   @RequestHeader long storeNo,
                                   @RequestHeader String accountId,
                                   @RequestBody UnitDto dto) {
        goodsUnitService.create(merchantNo, dto);
        return ResultJson.ok();
    }

    @PutMapping("/{id}")
    public ResultJson<Void> edit(@RequestHeader long merchantNo,
                                 @RequestHeader long storeNo,
                                 @RequestHeader String accountId,
                                 @PathVariable("id") String id,
                                 @RequestBody UnitDto dto) {
        goodsUnitService.edit(merchantNo, id, dto);
        return ResultJson.ok();
    }

    @DeleteMapping("/{id}")
    public ResultJson<Void> delete(@RequestHeader long merchantNo,
                                   @RequestHeader long storeNo,
                                   @RequestHeader String accountId,
                                   @PathVariable("id") String id) {
        goodsUnitService.delete(merchantNo, id);
        return ResultJson.ok();
    }

    @GetMapping("/{id}")
    public ResultJson<GoodsUnit> info(@RequestHeader long merchantNo,
                                      @RequestHeader long storeNo,
                                      @RequestHeader String accountId,
                                      @PathVariable("id") String id) {
        return ResultJson.ok(goodsUnitService.getById(id));
    }

    @GetMapping("/page")
    public ResultJson<PageVo<UnitVo>> page(@RequestHeader long merchantNo,
                                           @RequestHeader long storeNo,
                                           @RequestHeader String accountId,
                                           UnitQuery query) {
        return ResultJson.ok(goodsUnitService.page(merchantNo, null, query));
    }
}
